package org.pps.gl.client.vue;

import org.pps.gl.client.GetInfoService;
import org.pps.gl.client.GetInfoServiceAsync;
import org.pps.gl.client.LoginInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Button;

/**
 * Composite pour l'envoi d'un message à un utilisateur
 *
 */
public class FTR_NMessagesAdmin extends Composite {

	public FTR_NMessagesAdmin() {
		final DialogBox mailAdmin = new DialogBox();
		
		mailAdmin.setText("Assistance");
		
		FlexTable flexTable = new FlexTable();
		//initWidget(flexTable);
		flexTable.setSize("230px", "126px");

		Label lblNewLabel = new Label("Objet");
		flexTable.setWidget(0, 0, lblNewLabel);

		final TextBox textBox = new TextBox();
		flexTable.setWidget(0, 1, textBox);
		textBox.setWidth("210px");

		Label lblNewLabel_1 = new Label("Message");
		flexTable.setWidget(1, 0, lblNewLabel_1);

		final TextArea textArea = new TextArea();
		flexTable.setWidget(1, 1, textArea);
		textArea.setSize("210px", "72px");
		
		HorizontalPanel boutons = new HorizontalPanel();
		flexTable.setWidget(2, 1, boutons);
		Button envoyer = new Button("Envoyer");
		boutons.add(envoyer);
		// pour fermer
		envoyer.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				GetInfoServiceAsync gi = GWT
						.create(GetInfoService.class);
				String mail = new String();
				if (LoginInfo.getCurrentUser().getEmailAddress().isEmpty())
					mail = "visiteur";
				else
					mail = LoginInfo.getCurrentUser().getEmailAddress();
				//TODO administrateur
				gi.addMessage("timothee.quesada@gmail.com", mail, textArea.getText(), textBox.getText(),
						new AsyncCallback<Boolean>() {
						@Override
						public void onSuccess(Boolean result) {
							GWT.log("Message envoyé.");
						}
						@Override
						public void onFailure(Throwable caught) {
							GWT.log("Erreur lors de l'envoi du message.");
						}
					});
				mailAdmin.hide();
			}
		});
		
		Button annuler = new Button("Annuler");
		flexTable.setWidget(3, 1, annuler);
		// pour fermer
		annuler.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				mailAdmin.hide();
			}
		});
		boutons.add(annuler);
		
		mailAdmin.setWidget(flexTable);
		mailAdmin.setAnimationEnabled(true);

		Label b = new Label("Assistance");
		b.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				mailAdmin.center();
			}
		});
		initWidget(b);
	}

}
